package top.pengdong.pictureShare.tweet.controller;

import java.util.Arrays;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.pengdong.pictureShare.common.model.LoginVal;
import top.pengdong.pictureShare.common.model.PageUtils;
import top.pengdong.pictureShare.common.model.R;
import top.pengdong.pictureShare.common.utils.IPAddress;
import top.pengdong.pictureShare.common.utils.OauthUtils;
import top.pengdong.pictureShare.tweet.entity.PsCommentEntity;
import top.pengdong.pictureShare.tweet.entity.TUsersEntity;
import top.pengdong.pictureShare.tweet.service.PsCommentService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 评论表
 *
 * @author chenshun
 * @email pengshi12138@outlook.com
 * @date 2022-04-25 16:55:46
 */
@Slf4j
@RestController
@RequestMapping("api/comment")
public class PsCommentController {
    @Autowired
    private PsCommentService psCommentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = psCommentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PsCommentEntity psComment = psCommentService.getById(id);

        return R.ok().put("psComment", psComment);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PsCommentEntity psComment){
		psCommentService.updateById(psComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		psCommentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 基础文章操作
     */

    /**
     * 用户文章操作 添加评论
     */
    @RequestMapping(value = "/comment_add", method = RequestMethod.POST)
    public R comment_add (@RequestBody @Valid PsCommentEntity psComment, HttpServletRequest request) {
        String ipAddr = IPAddress.getIpAddr(request);
        log.info("ip 地址 :" + ipAddr + "添加评论操作");
        psComment.setLocation(ipAddr);
        return psCommentService.comment_add(psComment);
    }


    /**
     * 用户获取信息
     */
    @RequestMapping(value = "/comment_info", method = RequestMethod.GET)
    public R comment_info(@RequestParam(value = "aid", required = false) Long aid,
                          @RequestParam(value = "page", required = false) Integer page,
                          @RequestParam(value = "limit", required = false) Integer limit,
                          @RequestParam(value = "userId", required = false) Long userId) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        return psCommentService.get_comment_info(aid, page, limit, userId);
    }

    /**
     * 用户删除评论 点赞操作操作
     */
    @RequestMapping(value = "/comment_action", method = RequestMethod.GET)
    public R comment_action (@RequestParam(value = "cid", required = false) Long cid,
                             @RequestParam("ac") String action) {
        LoginVal loginVal = OauthUtils.getCurrentUser();
        if (loginVal == null) {
            return R.error(401, "用户没有登陆");
        }
        if ("like".equals(action)) {
            return psCommentService.add_like_comment(cid, Long.valueOf(loginVal.getUserId()));
        } else if ("delete_like".equals(action)) {
            return psCommentService.delete_like_comment(cid, Long.valueOf(loginVal.getUserId()));
        } else if ("delete".equals(action)) {
            return psCommentService.delete_comment(cid);
        }
        return R.error("参数错误");
    }
}
